// 存账户相关信息

package ATM;

import java.awt.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Random;

public class Account
{
    private String name;// 用户名
    private String card;// 卡号
    private String password;// 密码
    private double balance;// 余额
    private double maxx;// 可用额度

    public String getName() {
        return name;
    }

    public String getCard() {
        return card;
    }

    public void setCard(String card) {
        this.card = card;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public double getMaxx() {
        return maxx;
    }

    public void setMaxx(double maxx) {
        this.maxx = maxx;
    }

    public void setName(String name) {
        this.name = name;
    }

    Scanner sc = new Scanner(System.in);

    public void ShowAccount()
    {
        System.out.println("======用户信息=====");
        System.out.println("用户名" + name);
        System.out.println("卡号" + card);
        System.out.println("余额" + balance);
        System.out.println("可用额度" + maxx);
        System.out.println();
    }

    public boolean AddMoney(double count)
    {
        balance += count;
        System.out.println("存入成功！");
        ShowAccount();

        return true;
    }

    public boolean SubMoney(double count)
    {
        while (count > balance){
            System.out.println("取出失败，余额不足！");
            System.out.println("您的余额是：" + balance);
            System.out.println("请重新输入您要取的余额：");
            count = sc.nextDouble();
        }
        System.out.println("取出成功！");
        balance -= count;
        ShowAccount();

        return true;
    }

    public boolean TransferMoney(ArrayList<Account> list)
    {
        System.out.print("请输入您要转入的卡号：");
        String card = sc.next();
        Account dest = null;
        for (int i = 0; i < list.size(); i++)
        {
            dest = list.get(i);
            if (card == dest.card && dest != this)
                break;
        }
        if (dest == null)
        {
            System.out.println("没有查询到该卡号，转账失败!");
            return false;
        }

        if (balance == 0.0)
        {
            System.out.println("余额不足！\n您的余额为：0.0");
            return false;
        }
        double count = 0.0;
        while (true) {
            System.out.print("请输入您要转的数额:");
            count = sc.nextDouble();
            if (count > balance)
            {
                System.out.println("余额不足，您的余额为：" + balance);
            }
            else
                break;
        }

        String password;
        for (int i = 0 ;i < 3; i++) {
            System.out.println("请输入您的密码:");
            password = sc.next();

            if (password.equals(this.password))
            {
                System.out.println("密码正确，转账成功");
                SubMoney(count);
                dest.AddMoney(count);
                break;
            }
            else
            {
                if (i == 2)
                {
                    System.out.println("密码错误三次以上，登录失败");
                    return false;
                }
                System.out.print("密码错误，您还有" + (3 - 1 - i) + "次机会，请重新输入！");
            }
        }
        return true;
    }


    public boolean ChangePassword()
    {
        String oldpassword, newpassword1, newpassword2;

        for (int i = 0; i < 3; i++){
            System.out.println("请输入旧密码：");
            oldpassword = sc.next();
            if (oldpassword.equals(password))
                break;
            else {
                if (i == 2)
                {
                    System.out.println("密码错误三次以上，更改失败！");
                    return false;
                }
                System.out.print("密码错误，您还有" + (3 - 1 - i) + "次机会，请重新输入！");
            }
        }

        System.out.print("请输入新密码：");
        newpassword1 = sc.next();
        System.out.print("请重新输入新密码：");
        newpassword2 = sc.next();
        while (!newpassword1.equals(newpassword2))
        {
            System.out.println("两次密码不同，请重新输入！");

            System.out.print("请输入新密码：");
            newpassword1 = sc.next();
            System.out.print("请重新输入新密码：");
            newpassword2 = sc.next();
        }
        password = newpassword1;
        System.out.println("密码更改成功");

        return true;
    }
}

class AccountOperation
{
}